<?php

namespace App\Admin\Extensions;


use Encore\Admin\Admin;

class Offline
{
    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function script()
    {
        $token = csrf_token();
        $url = route('admin.product.auditing');
        return <<<SCRIPT

    $('.grid-offline-row').on('click', function () {

        var productId = $(this).attr('data-id');
        swal(
            {
                title: "确认下线该书籍?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                closeOnConfirm: false,
                cancelButtonText: "取消"
            },
            function(){
                $.ajax({
                    url: '$url',
                    dataType: 'json',
                    data: {
                        '_token': '$token',
                        'product_id': productId,
                        'operating': 'agree',
                        'process' : 'offline'
                    },
                    type: 'POST',
                    success: function (res) {
                        if (res.status == 'success') {
                            swal(res.msg, '', 'success');
                            $.pjax.reload('#pjax-container');
                        } else {
                            swal(res.msg, '', 'error');
                        }
                    }
                });
            }
        );
    });

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        return "&nbsp;<a href='javascript:void(0);' class='fa fa-level-down grid-offline-row' data-id='{$this->id}'></a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}